<?php
class Nadeb_Form
{
	private $primaryKey;
	private $_AllParams;
	private $rel;
	private $prop_id;
	private $prop_form;
	private $prop_objs;
	private $data_form;
	private $module;
	private $file;
	
	private $tagForm;
	
	public function __construct($_params)
	{
		$this->_AllParams   = $_params;
		$this->module		= $_params["module"];
		$this->file			= $_params["controller"];
		if( isset($_params["id"]) )
			$this->primaryKey	= $_params["id"];
		else
			$this->primaryKey	= null;
			
		if( isset($_params["rel"]) )
			$this->rel	= $_params["rel"];
		else
			$this->rel	= null;
	}
	
	private function create_form()
	{
		$configIni = new Nadeb_CrudConfig($this->module,$this->file);
		
		$this->prop_id   = $configIni->get_tableConfig();
		$this->prop_form = $configIni->get_tagFormConfig();
		$this->prop_objs = $this->set_obj_form( $configIni->get_inputsConfig() );
		if( $this->primaryKey ) 
		{
			$this->rewrite_action_form();
			$this->insert_primaryKeyInput();
		}
		
		if( $this->rel ) 
		{
			$this->insert_rel_on_form_action();
		}
		
		$this->tagForm = "<form";
		foreach($this->prop_form as $key => $value)
		{
			$this->tagForm .= ' ' . $key . '="' . $value . '"';
		}
		$this->tagForm .= ">\n\t<fieldset class='nadebDefault'>\n\t<dl>";
		
		foreach($this->prop_objs as $key => $array)
		{
			if( $array['type'] != "DateInsert" && $array['type'] != "DateUpdate")
			{
				$array = Nadeb_Form_Execute::functions($array,$this->_AllParams);
				$obj   = Nadeb_Form_Dataform::get_instance($array);
				$obj->create_object();
				
				$this->tagForm .= $obj->get_xhtml();
			}
		}
		$this->tagForm .= "\n\t</dl>\n\t</fieldset>\n\t</form>";
		
		return $this->tagForm;
		
	}
	
	public static function get_form($_params)
	{
		$obj = new Nadeb_Form($_params);
		return $obj->create_form();
	}
	
	private function set_obj_form($params)
	{
		$i= 0;
		foreach($params as $key => $array)
		{
			$obj[$i]["name"]  = $key;
			$obj[$i]["id"]    = $key;
			
			if( isset($this->primaryKey) )
			{
				foreach($array as $key => $value)
						$obj[$i][$key] = $value;
				
				$dados = $this->get_dados();
				if( isset( $dados[$obj[$i]["name"]] ) )
					$obj[$i]["value"] = $dados[$obj[$i]["name"]];
			}
			else
			{
				foreach($array as $key => $value)
					$obj[$i][$key] = $value;
			}
			$i++;
		}
		return $obj;
	}
	
	
	private function rewrite_action_form()
	{
		$this->prop_form["action"] = str_replace("insert","update", $this->prop_form["action"]);
	}
	
	private function insert_primaryKeyInput()
	{
		$var_input = array (
			"name"		=> "id",
			"id"		=> "id",
			"type"		=> "Hidden",
			"value"		=> "{$this->primaryKey}"
		);
		$this->prop_objs[] = $var_input;
	}
	
	private function insert_rel_on_form_action()
	{
		$this->prop_form["action"] = $this->prop_form["action"] . "/rel/" . $this->rel;
	}
	
	private function get_dados()
	{
		$db = Zend_Registry::get("db");
		$db->setFetchMode(Zend_Db::FETCH_ASSOC);
		
		$select = $db->select()
					 ->from( $this->prop_id['name'] )
					 ->where( $this->prop_id['key'] . ' = ?', $this->primaryKey );

		$dados = $db->fetchAll( $select );
		
		return $dados[0];
	}
	

}
